/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.laolei.core.rest.annotation;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.SingletonBeanRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;

import com.laolei.core.rest.HttpRequestHandle;
import com.laolei.core.rest.InvokeHandle;
import com.laolei.core.rest.ScannerAndRegistrarBean;
import com.laolei.core.rest.config.HostConfig;

@Component
@EnableConfigurationProperties({ HostConfig.class })
@ComponentScan(basePackageClasses = { ScannerAndRegistrarBean.class, HttpRequestHandle.class, InvokeHandle.class })
class RestClientProxyRegistrar implements ImportBeanDefinitionRegistrar {
	@Autowired
	ScannerAndRegistrarBean scannerAndRegistrarBean=new ScannerAndRegistrarBean();

	@Override
	public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
		Set<String> packages = getPackages(importingClassMetadata);
		SingletonBeanRegistry beanRegistry = (SingletonBeanRegistry) registry;
		scannerAndRegistrarBean.registrarScannerBean(packages, beanRegistry);
	}

	private Set<String> getPackages(AnnotationMetadata importingClassMetadata) {
		Map<String, Object> map = importingClassMetadata.getAnnotationAttributes(EnableRestClient.class.getName());
		Set<String> packages = new HashSet<>();
		String[] basePackages = (String[]) map.get("basePackages");
		String[] values = (String[]) map.get("value");
		Class<?>[] scanBasePackageClasses = (Class[]) map.get("scanBasePackageClasses");
		String classname = importingClassMetadata.getClassName();
		for (String basePackage : basePackages) {
			packages.add(basePackage);
		}
		for (String basePackage : values) {
			packages.add(basePackage);
		}
		for (Class<?> scanBasePackageClasse : scanBasePackageClasses) {
			packages.add(scanBasePackageClasse.getPackage().getName());
		}
		if (packages.size() == 0) {
			int endindex = classname.lastIndexOf(".");
			if (endindex != -1) {
				packages.add(classname.substring(0, endindex));
			}
		}
		return packages;
	}

}
